﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using urBook.Engine;

namespace urBook.Gui
{
    /// <summary>
    /// Interaction logic for AppendCountryCodeDialog.xaml
    /// </summary>
    public partial class AppendCountryCodeDialog : UserControl
    {
        public MainWindow root { get; set; }
        public PopupWindow popup { get; set; }
        List<ContactCard> affectedContacts;

        public AppendCountryCodeDialog(MainWindow root, List<ContactCard> affectedContacts)
        {
            InitializeComponent();

            this.root = root;
            this.popup  = new PopupWindow(root);
            this.affectedContacts = affectedContacts;

            loadCountries();
            root.Container.Children.Add(popup);
            popup.Container.Children.Add(this);
        }

        private void loadCountries()
        {
            Hashtable countries = Utilities.loadCountries(Properties.Resources.CountryCallingCodes);
            List<String> countriesList = new List<string>();
            foreach (string country in countries.Keys)
                countriesList.Add(country);
            countriesList.Sort();
            foreach (string country in countriesList)
                Countries.Items.Add(country + " (" + countries[country] + ")");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string selected = (String)Countries.SelectedItem;
            if (!String.IsNullOrEmpty(selected))
            {
                Apply.IsEnabled = false;
                string[] splits = selected.Split(new char[] { '(' });
                splits = splits[1].Split(new char[] { ')' });
                string countryCode = splits[0];

                AddressBook a = new AddressBook() { contacts = affectedContacts };
                a.appendCountryCode(countryCode);
                root.loadData();
                root.Container.Children.Remove(popup);
            }
            else
            {
                Information.Foreground = new SolidColorBrush(new Color() { ScA = 1, ScR = 1, ScG = 0, ScB = 0 });
            }
        }
    }
}
